<template>
  <div>
    <el-table
      :data="goods"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      :tree-props="{ children: 'children' }"
    >
      <el-table-column prop="id" label="商品编号" sortable width="180">
      </el-table-column>
      <el-table-column prop="goodsname" label="商品名称" sortable width="180">
      </el-table-column>
      <el-table-column prop="price" label="商品价格" sortable width="180">
      </el-table-column>
      <el-table-column
        prop="market_price"
        label="市场价格"
        sortable
        width="180"
      >
      </el-table-column>
      <el-table-column label="图片">
        <template v-slot="scope">
          <img
            v-if="scope.row.img !== ''"
            style="width: 100px; height: 100px"
            :src="$pregImg + scope.row.img"
            :alt="scope.row.catename"
          />
        </template>
      </el-table-column>
      <el-table-column prop="isnew" label="是否新品">
        <template v-slot="scope">
          <el-button v-if="scope.row.isnew === 1" type="success">是</el-button>
          <el-button v-else type="danger"> 否 </el-button>
        </template>
      </el-table-column>
      <el-table-column prop="ishot" label="是否热卖">
        <template v-slot="scope">
          <el-button v-if="scope.row.ishot === 1" type="success">是</el-button>
          <el-button v-else type="danger"> 否 </el-button>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template v-slot="scope">
          <el-button v-if="scope.row.status === 1" type="success"
            >启用</el-button
          >
          <el-button v-else type="danger"> 禁用 </el-button>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" @click="edit(scope.row.id)">编辑</el-button>
          <el-button type="danger" @click="del(scope.row.id)"> 删除 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      @current-change="cPage"
      :page-size="size"
    >
    </el-pagination>
  </div>
</template>

<script>
import { mapActions, mapGetters } from "vuex";
import { goodsManagerDel } from "../../../request/api";
import { successAlert } from "../../../utils/alert";
export default {
  computed: {
    ...mapGetters({
      goods: "goodsManager/goods",
      size: "goodsManager/size",
      total: "goodsManager/count",
    }),
  },
  methods: {
    ...mapActions({
      countActions: "goodsManager/countActions",
      goodsActions: "goodsManager/goodsActions",
      pageActions: "goodsManager/pageActions",
    }),
    cPage(page) {
      this.pageActions(page);
      this.goodsActions();
    },
    del(id) {
      this.$confirm("确定要删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 传一个对象
          goodsManagerDel({ id }).then((res) => {
            if (res.code === 200) {
              successAlert(res.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
      this.goodsActions();
    },
    edit(id){
      this.$emit("edit",id)
    }
  },
  mounted() {
    this.countActions();
    this.goodsActions();
  },
};
</script>